package com.antoco.operation.bigscreen.controller.modelcontroller.impl;

import com.antoco.operation.bigscreen.controller.modelcontroller.intf.PipeMateTjController;
import com.antoco.operation.bigscreen.model.dto.PipeMateTjDTO;
import com.antoco.operation.bigscreen.model.entity.PipeMateTj;
import com.antoco.operation.bigscreen.mapper.PipeMateTjMapper;
import com.antoco.operation.bigscreen.service.ChannelDataService;
import com.antoco.operation.bigscreen.service.autogeneration.intf.PipeMateTjService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/model/pipe-mate-tj")
@RestController
public class PipeMateTjControllerImpl implements PipeMateTjController {

    @Autowired
    private PipeMateTjService pipeMateTjService;

    @Autowired
    private PipeMateTjMapper pipeMateTjMapper;

    @Autowired
    private ChannelDataService channelDataService;

    @Override
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public PipeMateTjDTO save(@Validated @RequestBody PipeMateTjDTO pipeMateTjDTO) {
        PipeMateTj pipeMateTj = pipeMateTjMapper.asEntity(pipeMateTjDTO);
        pipeMateTjService.save(pipeMateTj);
        channelDataService.pushAllLists(this.getClass().getName());
        return pipeMateTjDTO;
    }

    @Override
    @GetMapping("/{id}")
    public PipeMateTjDTO findById(@PathVariable("id") String id) {
        PipeMateTj pipeMateTj = pipeMateTjService.findById(id).orElse(null);
        return pipeMateTjMapper.asDTO(pipeMateTj);
    }

    @Override
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") String id) {
        pipeMateTjService.deleteById(id);
        channelDataService.pushAllLists(this.getClass().getName());
    }

    @Override
    @GetMapping
    public List<PipeMateTjDTO> list() {
        return pipeMateTjMapper.asDTOList(pipeMateTjService.findAll());
    }

    @Override
    @GetMapping("/page-query")
    public Page<PipeMateTjDTO> pageQuery(Pageable pageable) {
        Page<PipeMateTj> pipeMateTjPage = pipeMateTjService.findAll(pageable);
        List<PipeMateTjDTO> dtoList = pipeMateTjPage
                .stream()
                .map(pipeMateTjMapper::asDTO).collect(Collectors.toList());
        return new PageImpl<>(dtoList, pageable, pipeMateTjPage.getTotalElements());
    }

    @Override
    @PutMapping("/{id}")
    public PipeMateTjDTO update(@RequestBody PipeMateTjDTO pipeMateTjDTO, @PathVariable("id") String id) {
        PipeMateTj pipeMateTj = pipeMateTjMapper.asEntity(pipeMateTjDTO);
        pipeMateTjMapper.asDTO(pipeMateTjService.update(pipeMateTj, id));
        channelDataService.pushAllLists(this.getClass().getName());
        return pipeMateTjDTO;
    }
}